package logic

import (
	"context"
	"errors"
	"log"
	"strconv"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper/request"
	"yundian/internel/pkg/authorized"
	"yundian/internel/app/models/usersteward"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/utils"
)

// ------------- 逻辑层logic - 权限相关 ------------------//

type StewardLogic struct {
}

func NewStewardLogic() *StewardLogic {
	return &StewardLogic{}
}

// UserLoginBySteward 公众号员工登录逻辑
func (l *StewardLogic) UserLoginBySteward(phone, password, openid string, ip string) (*request.LoginBack, error) {
	loginInfo := &request.LoginBack{
		Openid:   "",
		Token:    "",
		UserName: "",
		Avatar:   "",
	}
	user := dao.NewUserDao().GetStaffUserInfoFromPhone(phone).Unwrap().(*usersteward.StUserStaff)
	if openid == "" {
		openid = user.TencentOpenid
	}
	if openid == "" {
		return loginInfo, errors.New("微信未授权")
	}

	if user.Id == 0 {
		return loginInfo, errors.New("用户名密码错误")
	}

	if user.Password != utils.EncryptPassword(password) {
		return loginInfo, errors.New("密码错误")
	}

	if user.Status != 1 {
		return loginInfo, errors.New("该帐户已被禁用")
	}

	// 获取access_token
	wxTokenKey := utils.Join("WxAccess-Token-", openid)
	accessToken := global.Rdb.Get(context.Background(), wxTokenKey).Val()

	// 获取用户信息
	resp, _ := authorized.GetUserInfo(accessToken, openid)
	log.Println("获取用户信息", resp)

	user.TencentOpenid = openid
	user.Ip = ip

	dao.NewUserDao().UpdateStewardStaffUserById(user).Unwrap()

	// 生成 Token
	key := utils.Join("zm-steward-h5-Login-Token", strconv.Itoa(user.Id))
	token, err := dingo.GenerateToken(user.Id, user.Name, 0, user.ParentId, 0, 0, "")
	global.Rdb.Set(context.Background(), key, token, time.Hour*24*7) // 过期时间

	loginInfo = &request.LoginBack{
		Openid:   openid,
		Token:    token,
		UserName: resp.Nickname,
		Avatar:   resp.Headimgurl,
	}
	return loginInfo, err
}

// UserLoginByStewardApp APP员工登录逻辑
func (l *StewardLogic) UserLoginByStewardApp(phone, password, openid string, ip string) (*request.LoginBack, error) {
	loginInfo := &request.LoginBack{
		Openid:   "",
		Token:    "",
		UserName: "",
		Avatar:   "",
	}
	user := dao.NewUserDao().GetStaffUserInfoFromPhone(phone).Unwrap().(*usersteward.StUserStaff)
	if openid == "" {
		openid = user.TencentOpenid
	}

	if user.Id == 0 {
		return loginInfo, errors.New("用户名密码错误")
	}

	if user.Password != utils.EncryptPassword(password) {
		return loginInfo, errors.New("密码错误")
	}

	if user.Status != 1 {
		return loginInfo, errors.New("该帐户已被禁用")
	}

	// 获取access_token
	wxTokenKey := utils.Join("WxAccess-Token-", openid)
	accessToken := global.Rdb.Get(context.Background(), wxTokenKey).Val()

	// 获取用户信息
	resp, _ := authorized.GetUserInfo(accessToken, openid)
	log.Println("获取用户信息", resp)

	user.TencentOpenid = openid
	user.Ip = ip

	dao.NewUserDao().UpdateStewardStaffUserById(user).Unwrap()

	// 生成 Token
	key := utils.Join("zm-steward-h5-Login-Token", strconv.Itoa(user.Id))
	token, err := dingo.GenerateTokenApp(user.Id, user.Name, user.ParentId, user.MessageTypes)
	global.Rdb.Set(context.Background(), key, token, time.Hour*24*7) // 过期时间

	loginInfo = &request.LoginBack{
		Openid:   openid,
		Token:    token,
		UserName: resp.Nickname,
		Avatar:   resp.Headimgurl,
	}
	return loginInfo, err
}
